package com.naiterui.ehp.bs.consult.util;

import com.naiterui.ehp.bp.utils.date.DateUtil;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.Region;
import com.tencentcloudapi.trtc.v20190722.TrtcClient;
import com.tencentcloudapi.trtc.v20190722.models.DescribeRoomInformationRequest;
import com.tencentcloudapi.trtc.v20190722.models.DescribeRoomInformationResponse;
import com.tencentcloudapi.trtc.v20190722.models.DismissRoomRequest;
import com.tencentcloudapi.trtc.v20190722.models.DismissRoomResponse;
import com.tencentcloudapi.trtc.v20190722.models.RemoveUserRequest;
import com.tencentcloudapi.trtc.v20190722.models.RemoveUserResponse;
import lombok.extern.slf4j.Slf4j;

/**
 * 腾讯云实时音视频
 *
 * @author yangfuya
 * @version 1.0
 * @date 2021-02-25 10:06
 **/
@Slf4j
public class TrtcUtil {
    private static final String APPID = ConfigUtil.getString("txcloud.trtc.appId");
    private static final String SECRETID = ConfigUtil.getString("txcloud.trtc.secretId");
    private static final String SECRETKEY = ConfigUtil.getString("txcloud.trtc.secretKey");
    private static final Credential CRED = new Credential(SECRETID, SECRETKEY);

    public DescribeRoomInformationResponse getRoomInfoMation(String startTime, String endTime) {
        DescribeRoomInformationResponse resp = null;
        try {
            TrtcClient client = new TrtcClient(CRED, "ap-beijing");
            DescribeRoomInformationRequest req = new DescribeRoomInformationRequest();
            req.setStartTime(DateUtil.dateStr2Unix(startTime));
            req.setEndTime(DateUtil.dateStr2Unix(endTime));
            req.setSdkAppId(APPID);
            resp = client.DescribeRoomInformation(req);
        } catch (TencentCloudSDKException e) {
            log.warn("getRoomInfoMation is exception:{}", e.getMessage());
        }
        return resp;
    }

    /**
     * 移出用户
     *
     * @param roomId  房间号
     * @param userIds 用户id数据
     * @return RequestId
     */
    public static String removeUser(Integer roomId, String[] userIds) {
        String reqId = null;
        try {
            TrtcClient client = new TrtcClient(CRED, "ap-beijing");
            RemoveUserRequest req = new RemoveUserRequest();
            req.setSdkAppId(Long.parseLong(APPID));
            req.setRoomId(roomId.longValue());
            req.setUserIds(userIds);
            final RemoveUserResponse resp = client.RemoveUser(req);
            reqId = resp.getRequestId();
        } catch (TencentCloudSDKException e) {
            log.warn("remove user is exception:{}", e.getMessage());
        }
        return reqId;
    }

    /**
     * 解散房间
     *
     * @param roomId 房间号
     * @return 返回信息
     */
    public static DismissRoomResponse dismissRoom(Long roomId) {
        TrtcClient client = new TrtcClient(CRED, Region.Beijing.getValue());
        DismissRoomRequest req = new DismissRoomRequest();
        req.setSdkAppId(Long.parseLong(APPID));
        req.setRoomId(roomId);
        DismissRoomResponse resp = null;
        try {
            resp = client.DismissRoom(req);
        } catch (TencentCloudSDKException e) {
            log.warn("dismiss room is exception:{}", e.getMessage());
        }
        return resp;
    }

    /**
     * 解散房间
     *
     * @return 返回信息
     */
    public static DescribeRoomInformationResponse describeRoomInformation(Long startTime, Long endTime)
            throws TencentCloudSDKException {
        TrtcClient client = new TrtcClient(CRED, "ap-beijing");
        DescribeRoomInformationRequest req = new DescribeRoomInformationRequest();
        req.setSdkAppId(APPID);
        req.setStartTime(startTime);
        req.setEndTime(endTime);

        DescribeRoomInformationResponse resp = null;
        try {
            resp = client.DescribeRoomInformation(req);
        } catch (TencentCloudSDKException e) {
            log.warn("dismiss room is exception:{}", e.getMessage());
            throw e;
        }
        return resp;
    }

}
